/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import Entity.Admin;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MrKoi
 */
public class AdminBean {
    Collection listAdmin;
    Connect c=new Connect();
    Connection con;
    Classshare cs=new Classshare();
    CallableStatement cl=null;
    public AdminBean() {
    }
    private void closeCnn(){
        try {
            cl.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //login admin
    public boolean LoginAdmin(String user,String pass)
    {
        con=c.getConnection();
         try
         {
            String p=cs.MD5(pass);
            cl=con.prepareCall("{Call LoginAdmin_sp(?,?)}");
            cl.setString("userName",user);
            cl.setString("passWord",p);
            ResultSet rs=cl.executeQuery();
            if(rs.next())
            {
                 if(rs.getString("UserName").equals(user)&&rs.getString("PassWord").equals(p))
                 {
                    closeCnn();
                    return true;
                 }
                 else
                 {
                     closeCnn();
                     return false;
                 }
            }
            else{
                    closeCnn();
                     return false;
            }
         }catch (Exception ex){closeCnn(); return false;}
        //return false;
    }
    //add new admin
    public boolean InsertAdmin(String user,String pass,String fullname,String email,String address, String phone,int role)
    {
        con=c.getConnection();
        try
        {
            cl=con.prepareCall("{call InsertAdmin_sp(?,?,?,?,?,?,?)}");
            cl.setString("UserName",user);
            cl.setString("PassWord", pass);
            cl.setString("FullName", fullname);
            cl.setString("Email", email);
            cl.setString("Address", address);
            cl.setString("Phone", phone);
            cl.setInt("Role", role);
            int rs=cl.executeUpdate();
            if(rs>0)
            {
                closeCnn();
                return true;
            }
            closeCnn();
        }catch (Exception ex){closeCnn();return false;}
        return false;
    }
    //update information admin
     public boolean UpdateAdmin(String user,String fullname,String email,String address, String phone,int role)
    {
        con=c.getConnection();
        try
        {
            cl=con.prepareCall("{call UpdateAdmin_sp(?,?,?,?,?,?)}");
            cl.setString("UserName",user);
            cl.setString("FullName", fullname);
            cl.setString("Email", email);
            cl.setString("Address", address);
            cl.setString("Phone", phone);
            cl.setInt("Role", role);
            int rs=cl.executeUpdate();
            if(rs>0)
            {
                closeCnn();
                return true;
            }
            closeCnn();
        }catch (Exception ex){return false;}
        return false;
    }
     //delete admin
     public boolean DeleteAdmin(String user)
     {
         con=c.getConnection();
         try
         {
             cl=con.prepareCall("{call DeleteAdmin_sp(?)}");
            cl.setString("UserName",user);
            int rs=cl.executeUpdate();
            if(rs>0)
            {
                closeCnn();
                return true;
            }
            closeCnn();
         }catch (Exception ex){return false;}
         return false;
     }
    //select list admin
    public Collection SelectAdmin()
    {
        listAdmin=new ArrayList();
        con=c.getConnection();
        try
        {
            cl=con.prepareCall("{call SelectAdminAll_sp}");
            ResultSet rs=cl.executeQuery();
            while(rs.next())
            {
                Admin ad=new Admin();
                ad.setUserName(rs.getString("UserName"));
                ad.setFullName(rs.getString("FullName"));
                ad.setEmail(rs.getString("Email"));
                ad.setAddress(rs.getString("Address"));
                ad.setPhone(rs.getString("Phone"));
                ad.setRole(rs.getInt("Role"));
                listAdmin.add(ad);
            }
            closeCnn();
        }catch (Exception ex){}
        return listAdmin;
    }
    public static void main5(String[] args){
        AdminBean a= new AdminBean();
        ArrayList<Admin> arrAdmin= new ArrayList<Admin>();
        arrAdmin= (ArrayList<Admin>)a.SelectAdmin();
        if(arrAdmin.size()>0)
        System.out.println(arrAdmin.get(0).getFullName());
        else{
            System.out.println("Ko ton tai");
        }
    }
}
